﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;
using System.Windows.Input;

namespace XWord.WPF.ApplicationFramework.Controls
{
    public class SearchComboBox : ComboBox
    {
        public SearchComboBox()
        {
            this.IsEditable = true;
            this.IsTextSearchEnabled = false;
        }

        private TextBox _searchTextBox;

        public override void OnApplyTemplate()
        {

            base.OnApplyTemplate();
            _searchTextBox = base.GetTemplateChild("PART_EditableTextBox") as TextBox;
            _searchTextBox.BorderThickness = new Thickness(0);
            _searchTextBox.BorderBrush = Brushes.Transparent;
            
            _searchTextBox.KeyDown += new KeyEventHandler(_searchTextBox_KeyDown);
            return;
            _searchTextBox.TextChanged += delegate
            {


                this.Dispatcher.BeginInvoke(new Action(() =>
                {
                    this.Items.Filter = new Predicate<object>((object obj) =>
                    {
                        return obj.ToString().Contains(_searchTextBox.Text.Trim());
                    });
                    if (_searchTextBox.IsFocused)
                    {
                        this.IsDropDownOpen = true;
                    }
                }), System.Windows.Threading.DispatcherPriority.Render);
            };
        }

        protected override void OnSelectionChanged(SelectionChangedEventArgs e)
        {
            this.Focus();
            base.OnSelectionChanged(e);
           
        }

        protected override void OnDropDownOpened(EventArgs e)
        {
            _searchTextBox.SelectionStart = _searchTextBox.Text.Length;
            base.OnDropDownOpened(e);
        }

      

        protected override void OnDropDownClosed(EventArgs e)
        {
            this.Items.Filter = new Predicate<object>((object obj) =>
            {
                return true;
            });
            base.OnDropDownClosed(e);
        }

        void _searchTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                if (XWordAppConfig.Current.LatestSearchList.Contains(this.Text))
                {
                    XWordAppConfig.Current.LatestSearchList.Remove(this.Text);
                    XWordAppConfig.Current.LatestSearchList.Add(this.Text);
                    XWordAppConfig.Current.Save();
                }
                else
                {
                    XWordAppConfig.Current.LatestSearchList.Add(this.Text);
                    XWordAppConfig.Current.Save();
                }
            }
        }

        public event TextChangedEventHandler TextChanged
        {
            add
            {
                this.AddHandler(TextBox.TextChangedEvent, value);
            }
            remove
            {
                this.RemoveHandler(TextBox.TextChangedEvent, value);
            }
        }
    }
}
